﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MathTest
{
    public partial class ViewTheResultsForm : Form
    {
        #region 属性
        /// <summary>
        /// 测试结果集实例
        /// </summary>
        private List<Entity.TestResults> testResultsList = new List<Entity.TestResults>();
        /// <summary>
        /// 序列化实例
        /// </summary>
        private IFormatter formatter = new BinaryFormatter();

        private Entity.TestResults entiyTestResults = new Entity.TestResults();
        #endregion
        #region 构造函数
        public ViewTheResultsForm()
        {
            InitializeComponent();
        }
        #endregion
        #region 事件
        private void ViewTheResultsForm_Load(object sender, EventArgs e)
        {
            if (File.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log.txt"))
            {
                //读取实体类集
                Stream readstream = new FileStream(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
                testResultsList = (List<Entity.TestResults>)formatter.Deserialize(readstream);
                readstream.Close();
            }
            if (testResultsList.Count > 0)
            {
                dataGridView1.Rows.Add(testResultsList.Count);
                for (int i = 0; i < testResultsList.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = testResultsList[i].ID;
                    dataGridView1.Rows[i].Cells[1].Value = testResultsList[i].studentsName;
                    dataGridView1.Rows[i].Cells[2].Value = testResultsList[i].testDateTime;
                    dataGridView1.Rows[i].Cells[3].Value = testResultsList[i].TestSumCount;
                    dataGridView1.Rows[i].Cells[4].Value = testResultsList[i].testCorrectCount;
                    dataGridView1.Rows[i].Cells[5].Value = testResultsList[i].testCorrectRatio;
                }

            }
            else
            {
                MessageBox.Show("没有考试结果。", this.Text);
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Cells[0].Value != null)
            {
                string resultstr = "";
                List<Entity.TestResults> a = (from entity in testResultsList
                                              where entity.ID == dataGridView1.CurrentRow.Cells[0].Value.ToString()
                                              select entity).ToList();
                entiyTestResults = a[0];
                if (entiyTestResults.TestResultsItemList.Count > 0)
                {
                    foreach (var item in a[0].TestResultsItemList)
                    {
                        resultstr = resultstr + item.testQuestions + "  " + item.testResult + "\r\n";
                    }
                }
                MessageBox.Show("答题完毕。\r\n" + resultstr + "\r\n总题数:" + entiyTestResults.TestSumCount.ToString() + " 正确数量：" + entiyTestResults.testCorrectCount.ToString() + " 正确率:" + entiyTestResults.testCorrectRatio, this.Text);
            }
        }

        #endregion
    }
}
